package com.isorf.controller;

import com.github.pagehelper.PageInfo;
import com.isorf.pojo.shoe;
import com.isorf.pojo.type;
import com.isorf.service.Linkservice;
import com.isorf.service.UploadService;
import com.isorf.service.shoeservice;
import com.isorf.util.responseresult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class shoecontroller {

    @Autowired
    private Linkservice linkservice;
    @Autowired
    private UploadService uploadService;
    @Autowired
    private shoeservice shoeservice;
    @Value("${file.upload.path}")     // 从application.yml文件中获取图片上传的路径
    private String path;

    // 1. 分页查询
    @GetMapping("/pageList")
    public responseresult<PageInfo<shoe>> pageList(@RequestParam(defaultValue = "1") int pageNum,
                                                   @RequestParam(defaultValue = "5") int pageSize) {
        // 添加空值检查
        if (shoeservice == null) {
            return responseresult.failure(responseresult.FAILED, "服务未正确注入");
        }

        PageInfo<shoe> result = shoeservice.pageList(pageNum, pageSize);
        if (result != null && result.getList().size() > 0) {
            return responseresult.success(responseresult.SUCCESS, "查询成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "查询失败");
        }
    }

    // 2. 根据ID查询
    @GetMapping("/getShoeById/{shoeId}")
    public responseresult<shoe> getShoeById(@PathVariable Integer shoeId) {
        if (shoeId == null) {
            return responseresult.failure(responseresult.FAILED, "鞋子ID不能为空");
        }

        List<shoe> result = shoeservice.getShoeById(shoeId);
        if (result != null && result.size() > 0) {
            return responseresult.success(responseresult.SUCCESS, "查询成功", result.get(0));
        } else {
            return responseresult.failure(responseresult.FAILED, "查询失败");
        }
    }

    // 3. 修改鞋子信息
    @PostMapping("/updateshoe")
    public responseresult<Boolean> updateshoe(@RequestBody shoe shoe) {
        if (shoe == null || shoe.getShoeId() == null) {
            return responseresult.failure(responseresult.FAILED, "更新失败，缺少必要参数");
        }

        try {
            Boolean result = shoeservice.updateshoe(shoe);
            if (result) {
                return responseresult.success(responseresult.SUCCESS, "更新成功", result);
            } else {
                return responseresult.failure(responseresult.FAILED, "更新失败");
            }
        } catch (Exception e) {
            return responseresult.failure(responseresult.FAILED, "更新过程中发生错误: " + e.getMessage());
        }
    }

    // 4. 修改禁用状态
    @PostMapping("/updateShoeStatus")
    public responseresult<Boolean> updateShoeStatus(@RequestParam Integer shoeId,
                                                    @RequestParam Integer shoeStatus) {
        if (shoeId == null || shoeStatus == null) {
            return responseresult.failure(responseresult.FAILED, "参数不能为空");
        }

        try {
            Boolean result = shoeservice.updateShoeStatus(shoeId, shoeStatus);
            if (result) {
                return responseresult.success(responseresult.SUCCESS, "状态修改成功", result);
            } else {
                return responseresult.failure(responseresult.FAILED, "状态修改失败");
            }
        } catch (Exception e) {
            return responseresult.failure(responseresult.FAILED, "状态修改过程中发生错误: " + e.getMessage());
        }
    }

    // 5. 新增鞋子
    @PostMapping("/addshoe")
    public responseresult<Boolean> addshoe(@RequestBody shoe shoe) {
        if (shoe == null) {
            return responseresult.failure(responseresult.FAILED, "鞋子信息不能为空");
        }

        try {
            Boolean result = shoeservice.addshoe(shoe);
            if (result) {
                return responseresult.success(responseresult.SUCCESS, "新增成功", result);
            } else {
                return responseresult.failure(responseresult.FAILED, "新增失败");
            }
        } catch (Exception e) {
            return responseresult.failure(responseresult.FAILED, "新增过程中发生错误: " + e.getMessage());
        }
    }

    // 批量删除（保留原有功能）
    @PostMapping("/baaaaatchDelete")
    public responseresult<Boolean> batchDelete(@RequestBody List<Integer> ids) {
        // 检查参数
        if (ids == null || ids.isEmpty()) {
            return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
        }

        // 调用service层的批量删除方法
        Boolean result = linkservice.batchDelete(ids);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "批量删除成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "批量删除失败");
        }
    }
}
